#ifndef VBLANK_H
#define VBLANK_H

#include "vstyle.h"
#include "vcontrol.h"

namespace
{
	const QString SCRIPT_HEADER = "import Qt 4.7\nimport " + VCTRL_LIB_NAME + " " + QString("%1.%2\n\n").arg(VBLANK_MAJ_V).arg(VBLANK_MIN_V) + VCTRL_BLANK + " {\n";
	const QString SCRIPT_FOOTER = "}\n";
}

class VBlank : public QObject
{
	Q_OBJECT
	Q_PROPERTY(QDeclarativeListProperty<VStyle> styles READ styles)
	Q_PROPERTY(QDeclarativeListProperty<VAbstractControl> controls READ controls)
	Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
	Q_CLASSINFO("DefaultProperty", "controls")

public:
	VBlank(QObject* parent=0);
	~VBlank();

public:
	enum Mode
	{
		EditMode,
		FillMode
	};

public:
	// deprecated
	void createControl(VAbstractControl::Type controlType);
	void deleteControl(const QString& name);
	void appendControl(VAbstractControl* c);
	void setControlParent();

	QList<VStyle*> styleList() const;
	VStyle* findStyleByName(const QString& name);
	
	VAbstractControl* findControlByName(const QString& name);
	VAbstractControl* findControlByWidget(const QWidget& w);
	QList<VAbstractControl*> controlList() const;
	QList<VAbstractControl*> controlWithFocusList() const;
	
	QString serialize() const;

	bool isEmpty() const
	{ 
		return m_controls.isEmpty();// (m_styles.isEmpty() && m_controls.isEmpty());
	}

	Mode mode() const { return m_mode; }
	void setMode(Mode m) { m_mode = m; }

	QColor backgroundColor() const { return m_bkgColor; }
	void setBackgroundColor(const QColor& color) { m_bkgColor = color; }

public:
	void createBlankSignal() { emit blankCreated(); }
	void destroyBlankSignal() { emit blankDestroyed(); }

public slots:
	void updateBlank() { emit blankChanged(); }
	void updateBlank(VAbstractControl* c)
	{ 
		//qDebug() << "LOST FOCUS";
		
		emit blankChanged(c);
		m_curFocusControl = c;

		if (m_curFocusControl == m_lastFocusControl)
			m_firstFocusControl->widget()->setFocus();
		//else if (m_curFocusControl == m_firstFocusControl)
		//	m_firstFocusControl->widget()->setFocus();
	}

private:
	QDeclarativeListProperty<VStyle> styles();
	QDeclarativeListProperty<VAbstractControl> controls();

	QString nextTextEditName();
	QString nextImageName();
	QString nextTableName();

signals:
	void blankChanged();
	void blankChanged(VAbstractControl* c);	
	void blankCreated();
	void blankDestroyed();

private:
	QList<VStyle*> m_styles;
	QList<VAbstractControl*> m_controls;
	Mode m_mode;
	QColor m_bkgColor;

	QList<VAbstractControl*> m_focusControlEditMode;
	QList<VAbstractControl*> m_focusControlFillMode;

	VAbstractControl* m_firstFocusControl;
	VAbstractControl* m_lastFocusControl;
	VAbstractControl* m_curFocusControl;

	int m_styleNum;
	int m_tableNum;
	int m_imageNum;
	int m_textEditNum;
};
//QML_DECLARE_TYPEINFO(VBlank, QML_HAS_ATTACHED_PROPERTIES)

#endif // VBLANK_H
